package com.student.web.student;

import com.google.gson.Gson;
import com.student.dao.impl.StudentDaoImpl;
import com.student.pojo.College;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 获得全部的学院信息
 * 用于制作<select>标签，和学院、专业二级联动
 * 返回College数组的json
 * @author lin945
 *
 */
@WebServlet("/getCollege")
public class GetCollegeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private StudentDaoImpl studentDao = new StudentDaoImpl();
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		if (session.getAttribute("username") == null) {
			response.sendRedirect(request.getContextPath() + "/login");
			return;
		}

		List<College> list = studentDao.queryAllCollege();
		response.setHeader("Content-Type", "application/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		String college = gson.toJson(list);
		out.print(college);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
